package com.huaao.micropolice.core.common;

import javax.servlet.http.HttpServletRequest;

import com.huaao.micropolice.core.model.system.User;

public class Context {
	
	/**
	 * 用户线程变量
	 */
	private static ThreadLocal<User> userHolder = new ThreadLocal<User>();
	
	public static void setCurrentUser(User user) {
		userHolder.set(user);
	}

	public static User getCurrentUser() {
		return userHolder.get();
	}

	public static void resetCurrentUser() {
		userHolder.remove();
	}
	
	public static String getCurrentUserId(HttpServletRequest request) {
		User user = getCurrentUser(request);
		return user != null ? user.getId() : null;
	}

	public static User getCurrentUser(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute(Constants.CURRENT_USER);
		return user;
	}

	public static void setCurrentUser(HttpServletRequest request, User user) {
		request.getSession().setAttribute(Constants.CURRENT_USER, user);
	}

	public static void resetCurrentUser(HttpServletRequest request) {
		request.getSession().removeAttribute(Constants.CURRENT_USER);
	}
}
